import type { ScmPurchaseDetail, ScmPurchaseOrder } from "@/pages/wms/po/entity";
import apiClient from "../../apiClient";

export interface PageListReq {
	//订单编码
	poCode?: string;
	//订单类型
	poType?: string;
	//供应商
	supplierId?: number;
	//订单状态
	status?: string;
	//订单日期
	orderDate?: Date;
	//当前页码
	page: number;
	//每页数量
	pageSize: number;
}

export interface PageListRes {
	//总记录数
	total: number;
	//数据列表
	records: ScmPurchaseOrder[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
	apiClient.post({
		url: "/wms/po/pageList",
		data,
	});

const getInfo = (id: React.Key): Promise<ScmPurchaseOrder> =>
	apiClient.get({
		url: `/wms/po/${id}`,
	});

const add = (data: ScmPurchaseOrder): Promise<ScmPurchaseOrder> =>
	apiClient.post({
		url: "/wms/po",
		data,
	});

const update = (data: ScmPurchaseOrder): Promise<ScmPurchaseOrder> =>
	apiClient.put({
		url: "/wms/po",
		data,
	});

const remove = (ids: React.Key[]): Promise<void> =>
	apiClient.delete({
		url: "/wms/po",
		params: {
			ids: ids.join(","),
		},
	});

const getDetails = (id: React.Key): Promise<ScmPurchaseDetail[]> =>
	apiClient.post({
		url: "/wms/poDetail/list",
		data: {
			purchaseId: id,
		},
	});

const updateStatus = (ids: React.Key[], status: string): Promise<void> =>
	apiClient.put({
		url: `/wms/po/status/${status}`,
		params: {
			ids: ids.join(","),
		},
	});

export const scmPurchaseOrderService = {
	pageList,
	getInfo,
	add,
	update,
	remove,
	getDetails,
	updateStatus,
};

export default scmPurchaseOrderService;
